function initCron(cycleType,timeArr) {

    var cron;
    var planTime = $('#planTime').val().split(':');
    var hour;
    var min;
    if (planTime != '') {
        hour = planTime[0].replace(/\b(0+)/gi, "");    // 去掉字符串前面的0
        min = planTime[1].replace(/\b(0+)/gi, "");
    }
    if (cycleType == 1) {
        $("input:checkbox[name='clock']:checked").each(function (i) {
            timeArr.push($(this).val());
        });
        if (timeArr.length == 0) {
            //如果是空则需要判断计划时间是否填写
            if ($('#planTime').val() == '') {
                layer.msg('计划时间或者计划日期中必须有一个有值');
                return false;
            } else {
                cron = String.format('0 {0} {1} * * ?', min, hour);
            }
        } else {
            cron = String.format('0 0 {0} * * ?', timeArr.join(','));
        }
    } else if (cycleType == 2) {
        $("input:checkbox[name='weekDay']:checked").each(function (i) {
            timeArr.push($(this).val());
        });

        if (timeArr.length == 0) {
            layer.msg('请至少选择一周里面的一天');
            return false;
        } else {
            cron = String.format('0 {0} {1} ? * {2}', min, hour, timeArr.join(','));
        }
    } else if (cycleType == 3) {
        $("input:checkbox[name='monthDay']:checked").each(function (i) {
            timeArr.push($(this).val());
        });
        if (timeArr.length == 0) {
            layer.msg('请至少选择一个月里面的一天');
            return false;
        } else {
            cron = String.format('0 {0} {1} {2} * ?', min, hour, timeArr.join(','));
        }
    }
    return cron;
}

function initCycleTaskContent(){
    var content = [];

    $("select[name='modelId'] option:selected").each(function(index,elem){
        var temp = {};
        temp.modelId = $(elem).val();
        temp.modelName = $(elem).text();
        var devicePartOption = $(elem).parent().parent().next().find('select[name="devicePartId"] option:selected');

        temp.devicePartId = $(devicePartOption).val();
        temp.devicePartName = $(devicePartOption).text();

        var compontentArry = [];
        var tempComponentIds = formSelects.value('component'+index, 'val');
        var tempComponentNames = formSelects.value('component'+index, 'name');
        for(var i in tempComponentIds){
            var compontentTemp = {};
            compontentTemp.componentId = tempComponentIds[i];
            compontentTemp.componentName = tempComponentNames[i];
            compontentArry.push(compontentTemp);
        }
        temp.components = compontentArry;
        content.push(temp);

    });

    console.log('content:',content);
    return JSON.stringify(content);
}

function initCycleTaskTime(cycleType,chekbox){
    JJZ.named('cycle').val(cycleType);
    if(cycleType == 1){

        $("input:checkbox[name='clock']").each(function(i){
            if($.inArray($(this).val(), chekbox)>-1){
                $(this).prop("checked", true);
            }
        });
    }else if(cycleType == 2){
        $("input:checkbox[name='weekDay']").each(function(i){
            if($.inArray($(this).val(), chekbox)>-1){
                $(this).prop("checked", true);
            }
        });
    }else if(cycleType == 3){
        $("input:checkbox[name='monthDay']").each(function(i){
            if($.inArray($(this).val(), chekbox)>-1){
                $(this).prop("checked", true);
            }
        });
    }
    form.render('checkbox');
}

function addOrderDetail(length,orderList){
    //先把第0个初始化
    JJZ.initMultipleSelect2(formSelects, 'component0', '/component/dataGridByPartId', 'partId=' + orderList[0].devicePartId, 'name', function (noError) {
        if(noError){
            var components = orderList[0].components;
            var selected = [];
            for(var i in components){
                selected.push(components[i].componentId);
            }
            formSelects.value('component0',selected);
        }
    });

    //先动态添加完成html
    for(var i = 1; i<length; i++){
        var orderDiv = $('#order').clone();
        var index = $('div.orderDetails').length;
        $(orderDiv).removeAttr("id");
        $(orderDiv).prepend('<label class="layui-form-label"></label>');
        $(orderDiv).find('select[name="devicePartId"]').attr('index', index);
        $(orderDiv).find('div.layui-inline.componentDiv').empty();
        var html = " <select name='component' class='component' xm-select='component" + index + "' xm-select-skin='normal' xm-select-show-count='2' lay-filter='component'>" +
            "               <option value=''>选择检查配件</option>" +
            "        </select>"
        $(orderDiv).find('div.layui-inline.componentDiv').append(html);
        $('div.orderDetails:last').after(orderDiv);
        formSelects.render('component' + index);

        JJZ.initMultipleSelect2(formSelects, 'component' + index, '/component/dataGridByPartId', 'partId=' + orderList[i].devicePartId, 'name', function (noError) {
            if(noError){
                var components = orderList[index].components;
                var selected = [];
                for(var i in components){
                    selected.push(components[i].componentId);
                }
                formSelects.value('component' + index,selected);
            }
        });


    }

    //初始化设备类型下拉框
    JJZ.initSelect(JJZ.named('modelId'), '/model/dataGrid',function(noError){
        //在这里初始化接下来的指令
        if(noError){
            JJZ.named('modelId').find('option').each(function(index,elem){
                var tempElem = elem.outerHTML
                if(index != 0){
                    for(var i = 1 ;i<length;i++){
                        if($(tempElem).val() == orderList[0].modelId){
                            var selectedOption = '<option value="'+$(tempElem).val()+'" selected="selected">'+$(tempElem).text()+'</option>';
                            $('div.orderDetails').eq(i).find('select[name="modelId"]').append(selectedOption);
                        }else{
                            $('div.orderDetails').eq(i).find('select[name="modelId"]').append(tempElem);
                        }
                    }
                }
                $(elem).val() == orderList[0].modelId ? $(elem).attr("selected", "selected"):'';
            })

        }
    });

    //初始化设备部位下拉框
    JJZ.initSelect(JJZ.named('devicePartId'), '/devicePart/dataGrid',function(noError){
        if(noError){
            JJZ.named('devicePartId').find('option').each(function(index,elem){
                var tempElem = elem.outerHTML
                if(index != 0){
                    for(var i = 1 ;i<length;i++){
                        if($(tempElem).val() == orderList[0].devicePartId){
                            var selectedOption = '<option value="'+$(tempElem).val()+'" selected="selected">'+$(tempElem).text()+'</option>'
                            $('div.orderDetails').eq(i).find('select[name="devicePartId"]').append(selectedOption);
                        }else{
                            $('div.orderDetails').eq(i).find('select[name="devicePartId"]').append(tempElem);
                        }
                    }
                }
                $(elem).val() == orderList[0].devicePartId ? $(elem).attr("selected", "selected"):'';
            })
        }
    });
    form.render();
}
